package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public R handlerException(SQLIntegrityConstraintViolationException e){
        String message = e.getMessage();
        if (message.contains("Duplicate entry")){
            String username = message.split(" ")[2];
            return R.error(username+"改用户名已存在");
        }
        return R.error("未知错误");
    }
    @ExceptionHandler(CustomerException.class)
    public R handlerCustomerException(CustomerException e){
        String message = e.getMessage();
        return R.error(message);
    }
}
